public class LongestCommonPrefix {
    /*
    编写一个函数来查找字符串数组中的最长公共前缀。
    如果不存在公共前缀，返回空字符串 ""。
     */
    public  static String longestCommonPrefix(String[] strs) {
        /*
        1 先找到最短的一项
        2 再从最短的一项里找最大的
         */
        int str_min_len = strs[0].length();
        int flag = 0;
        String longestCommonPrefix = "";

        for(int i=0;i<strs.length;i++){
            if (strs[i].length()<str_min_len){
                str_min_len = strs[i].length();
                flag =i;
            }
        }
        char[] chars = strs[flag].toCharArray();
        for (int i=0;i<chars.length;i++){

            for(int j=0;j<strs.length;j++){
               char[] temp_chars = strs[j].toCharArray();
               if (temp_chars[i] != chars[i]){
                   return longestCommonPrefix;

               }
            }
            longestCommonPrefix = longestCommonPrefix+chars[i];
        }
         return longestCommonPrefix;
    }

    public static void main(String[] args) {
        String[] str = {"flower","flow","flight"};
        System.out.printf("str数组的最长公共前缀是%s",longestCommonPrefix(str));


    }

}
